<?php
namespace app\admin\controller;

class Config extends Common{

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {

        $ConfigModel = new \app\admin\model\Config();
        $list = $ConfigModel->all();
        $this->assign([
            'config' => $list,
            'domain' => $list['domain']
        ]);
        return $this->fetch();
    }


    public function update()
    {
        /**
         * 参数说明
         * title 标题
         * content 自动回复内容
         */
        $input = ArrRemoveJs(input('post.'));


        //上传图片
        if(($input['userPhotoOnLine'] = $this->upload_photo('userPhotoOnLine')) === false)
        {
            $this->error('用户上线头像上传失败');
        }
        if(($input['userPhotoOffLine'] = $this->upload_photo('userPhotoOffLine')) === false)
        {
            @unlink("./../".$input['userPhotoOnLine']);
            $this->error('用户下线头像上传失败');
        }
        if(($input['kfPhotoOnLine'] = $this->upload_photo('kfPhotoOnLine')) === false)
        {
            @unlink("./../".$input['userPhotoOnLine']);
            @unlink("./../".$input['userPhotoOffLine']);
            $this->error('客服上线头像上传失败');
        }
        if(($input['kfPhotoOffLine'] = $this->upload_photo('kfPhotoOffLine')) === false)
        {
            @unlink("./../".$input['userPhotoOnLine']);
            @unlink("./../".$input['userPhotoOffLine']);
            @unlink("./../".$input['kfPhotoOnLine']);
            $this->error('客服下线头像上传失败');
        }


        $ConfigModel = new \app\admin\model\Config();
        $ConfigModel->edit($input);
        $this->success("配置完成",url('index'));
    }


    public function upload_photo($field,$size = 2097152)
    {

        $this->request = request();

        $file = $this->request->file($field);
        $path =  'public' . DS . 'uploads'. DS . "config";
        if(empty($file)) return '';
        $info = $file->validate(['size'=>$size,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . $path);
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            $img_path = str_replace('\\','/',$path.DS.$info->getSaveName());
            return $img_path;
        }else{
            return false;
        }
    }

    //新增config配置
    public function add()
    {
        /**
         * 参数说明
         * post.cKey
         * post.cValue
         */
        $input = ArrRemoveJs(input('post.'));
        if(($path = $this->upload_photo('cValue')) === false)
        {
            @unlink("./../".$input['userPhotoOnLine']);
            @unlink("./../".$input['userPhotoOffLine']);
            @unlink("./../".$input['kfPhotoOnLine']);
            $this->error('图片上传失败');
        }
        elseif($path != '')
        {
            $input['cValue'] = $path;
        }

        $configModel = new \app\admin\model\Config();
        $result =  $configModel->add($input);
        if($result['error'] != 0)
        {
            @unlink('./../'.$path);
        }
        $this->success($result['msg'],url('index'));
    }

}